<?php
namespace Admin\Controller;
use \Admin\Model\UserModel;
use \Admin\Controller\SafeController;
include "./Common/CheckAdmin.php";

final class UserController{
	//显示用户列表
	public function showUser(){
		//实例化UserModel对象
		$userModelObj = new UserModel();
		
		//以下为分页的代码
		//每页显示条数
		$pagesize = $GLOBALS['config']['pagesize'];
		//获取当前页面
		$page = isset($_GET['page'])?$_GET['page']:1;
		//获取开始行号(每一页的第一行应该是这一页页数-1然后乘以每页大小)
		$startrows = ($page-1)*$pagesize;
		//获取总记录数,数据库rowcount
		$rows = $userModelObj->rowCount();
		//计算总页数（所有行数，除以每页数量就应该是总行数。但是必须上取整，因为最后一页可能不足$pagesize的数量）
		$pages = ceil($rows/$pagesize);
		//开始页码
		//结束页码
		$start = $page-5;
		$end = $page+4;
		if($page<=6){
			$start = 1;
			$end = 10;
		}
		if($page>=$pages-4){
			$start = $pages-9;
			$end = $pages;
		}
		if($pages<10){
			$start = 1;
			$end = $pages;
		}

		//分页获取记录
		$arrs = $userModelObj->fetchByPages($startrows,$pagesize);	
		//引入视图文件
		include VIEW_PATH."showUser.html";
	}
	
	//加载编辑用户视图
	public function editUserView(){
		$id = $_GET['id'];
		$userModelObj = new UserModel();
		$arr = $userModelObj->fetchOne($id);
		include VIEW_PATH."editUser.html";
	}
	
	//编辑用户
	public function editUser(){
		$uid = $_POST['id'];
		$acc = $_POST['account'];
		$name = $_POST['name'];
		$sex = $_POST['sex'];
		$phone = $_POST['phone'];
		$qq = $_POST['qq'];
		$email = $_POST['email'];
		//实例化UserMOdel对象
		$userModelObj = new UserModel();
		$row = $userModelObj->editUser($uid,$acc,$name,$sex,$phone,$qq,$email);
		
		if($row==1){
			echo 200;
		}
		else{
			echo 500;
		}
	}
	
	//删除用户
	public function delUser(){
		$id = $_GET['id'];
		$userModelObj = new UserModel();
		$row = $userModelObj->delUser($id);
		if($row==1){
			$mes = [
				"code"=>200,
				"msg"=>"删除成功<br>",
				"url"=>"?c=User&a=showUser"
			];
			$json =  json_encode($mes);
			echo $json;
		}
	}
	
	//加载新增用户视图
	public function addUserView(){
		include VIEW_PATH."addUser.html";
	}
	
	//新增用户
	public function addUser(){
		$account = $_POST['account'];
		$password = $_POST['password'];
		$name = $_POST['name'];
		$sex = $_POST['sex'];
		$phone = $_POST['phone'];
		$qq = $_POST['qq'];
		$email = $_POST['email'];
		
		$safeControllerObj = new SafeController();
		//$password = $safeControllerObj -> filterWords($password);
		//md5散列
		$password = md5($password);
		$userModelObj = new UserModel();
		$row = $userModelObj->addUser($account,$password,$name,$sex,$phone,$qq,$email);
		if($row==1){
			echo 200;
		}
		else{
			echo 500;
		}
	}
	
	//判断用户名是否存在
	public function isExist(){
		$acc = $_POST['account'];
		$userModelObj = new UserModel();
		$row = $userModelObj->isExist($acc);
		if($row==0){
			echo 200;
		}
			else{
			echo 500;
		}
	}
	
	//搜索用户
	public function searchUser(){
		$userModelObj = new UserModel();
		$key = $_POST['keyword'];
		$by = $_POST['by'];
		switch ($by){
			case 'uid':
				$arrs = $userModelObj->searchUserByUid($key);
				break;
			case 'username':
				$arrs = $userModelObj->searchUserByAccount($key);
				break;
			case 'phone':
				$arrs = $userModelObj->searchUserByPhone($key);
				break;
			case 'email':
				$arrs = $userModelObj->searchUserByEmail($key);
				break;
		}

		include VIEW_PATH."showUser.html";
	}
	
	
	
}
?>